package com.pabble.webnative;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.webkit.GeolocationPermissions;
import android.webkit.PermissionRequest;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class ChromeClient extends WebChromeClient {
    private ValueCallback<Uri[]> pickPhoto;
    private String mCapturePath;
    private ProgressBar progressBar = null;

    public void onProgressChanged(WebView view, int newProgress) {
        Log.i(WebActivity.TAG,"加载进度:"+newProgress);
        if(progressBar != null){
            progressBar.setProgress(newProgress);
        }
    }

    public void setProgressBar(ProgressBar bar){
        progressBar = bar;
    }
    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        String[] types = fileChooserParams.getAcceptTypes();
        if(fileChooserParams.isCaptureEnabled()){
            takeCamera();
        } else if(types.length > 0){
            takePhoto(types[0]);
        } else {
            Toast.makeText(WebActivity.Instance, "accept is null", Toast.LENGTH_SHORT).show();
        }
        pickPhoto = filePathCallback;
        return true;
    }
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        boolean apply = WebActivity.Instance.mBridge.checkPermission("android.permission.ACCESS_COARSE_LOCATION,android.permission.ACCESS_FINE_LOCATION") == "";
        callback.invoke(origin,apply,false);;
    }
    @Override
    public void onPermissionRequest(PermissionRequest request) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            request.grant(request.getResources());
        }
    }

    @Deprecated
    public void openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture) {

        takePhoto("image/*");
    }

    private void takePhoto(String type) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, type);
        WebActivity.Instance.startActivityForResult(intent, Bridge.PHOTO_REQUEST);
    }

    private void takeCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
        File file = new File(filePath, System.nanoTime() + ".jpg");

        Uri uri = FileProvider.getUriForFile(WebActivity.Instance,  WebActivity.Instance.getPackageName()+".fileProvider", file);
        mCapturePath = file.getAbsolutePath();

        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        WebActivity.Instance.startActivityForResult(intent, Bridge.CAMERA_REQUEST);
    }

    protected void onActivityResult(int requestCode,int resultCode, Intent data){
        if(requestCode == Bridge.PHOTO_REQUEST){
            if(resultCode == RESULT_OK){
                pickPhoto.onReceiveValue(new Uri[]{data.getData()});
            } else {
                pickPhoto.onReceiveValue(null);
            }
        } else if(requestCode == Bridge.CAMERA_REQUEST){
            if(resultCode == RESULT_OK){
                Uri uri = Uri.fromFile(new File(mCapturePath));
                pickPhoto.onReceiveValue(new Uri[]{uri});
            } else {
                pickPhoto.onReceiveValue(null);
            }
        }
    }
}
